#!/usr/bin/env bash

# 将 Rust SDK 编译为鸿蒙架构的 .a

# 配置环境变量，保证能找到 aarch64-unknown-linux-ohos-clang
if [ -z "${RC_OPEN_OHOS_PATH}" ]; then
    echo "error: RC_OPEN_OHOS_PATH not found"
    echo "eg."
    echo "export RC_OPEN_OHOS_PATH=\"/Applications/DevEco-Studio.app/Contents/sdk/HarmonyOS-NEXT-DB1/openharmony\""
    echo "export RC_OPEN_OHOS_PATH=\"/root/.harmony/commandline-Linux-5.0.3.401/command-line-tools/sdk/HarmonyOS-NEXT-DB1/openharmony\""
    exit 1
fi

# 进入 rust 项目路径
cd my_rust_sdk/

echo "编译鸿蒙开始"
# 指定鸿蒙 clang 路径
export CC=${RC_OPEN_OHOS_PATH}/native/llvm/bin/aarch64-unknown-linux-ohos-clang
# 制动鸿蒙头文件
export C_INCLUDE_PATH=${RC_OPEN_OHOS_PATH}/native/sysroot/usr/include:${RC_OPEN_OHOS_PATH}/native/sysroot/usr/include/aarch64-linux-ohos
# rust 编译鸿蒙 arm64 架构
cargo build -Zbuild-std -p rust_sdk --release --target aarch64-unknown-linux-ohos --verbose
cd -
echo "编译鸿蒙结束"

# 头文件复制到鸿蒙 my_lib 里面
cp -f my_rust_sdk/examples/c_demo/rcim_client.h my_lib/src/main/cpp/
# rust 编译出来的鸿蒙 arm64 静态库复制到 my_lib 里面
if [ ! -d "my_lib/src/main/cpp/libs/arm64-v8a/" ]; then
  mkdir -p "my_lib/src/main/cpp/libs/arm64-v8a/"
fi
cp -f my_rust_sdk/target/aarch64-unknown-linux-ohos/release/librust_sdk.a my_lib/src/main/cpp/libs/arm64-v8a/